package com.hirsi.common.core.exception;

import cn.hutool.core.collection.CollUtil;
import com.hirsi.common.core.enums.IVoDataValidEnum;
import com.hirsi.common.core.vo.result.FieldValidResult;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@NoArgsConstructor
public class VoDataValidError implements IExceptionEnum {

    private HttpStatus status = HttpStatus.BAD_REQUEST;

    private Integer code = 400;

    private String msg;

    private List<FieldValidResult> data;

    public VoDataValidError(String msg) {
        this.msg = msg;
    }

    public VoDataValidError(IVoDataValidEnum data) {
        this.msg = data.getMsg();
        this.data = Collections.singletonList(new FieldValidResult(data.getField(), data.getMsg()));
    }

    public VoDataValidError(IVoDataValidEnum... data) {
        this.msg = data[0].getMsg();
        this.data = CollUtil.newArrayList(data).stream().map(item -> new FieldValidResult(item.getField(), item.getMsg())).collect(Collectors.toList());
    }

    public VoDataValidError(List<IVoDataValidEnum> data) {
        this.msg = data.get(0).getMsg();
        this.data = data.stream().map(item -> new FieldValidResult(item.getField(), item.getMsg())).collect(Collectors.toList());
    }

    public VoDataValidError(String msg, List<IVoDataValidEnum> data) {
        this.msg = msg;
        this.data = data.stream().map(item -> new FieldValidResult(item.getField(), item.getMsg())).collect(Collectors.toList());
    }
}
